package com.lss.cloud.Controller;

import com.lss.cloud.Resp.ResultData;
import com.lss.cloud.entities.Pay;
import com.lss.cloud.entities.PayDTO;
import com.lss.cloud.service.PayService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 2024/3/13-19:41
 * 功能:
 */
@RestController
@Slf4j
@Tag(name = "支付微服务模块",description = "支付CRUD")
public class PayController {
    @Autowired
    private PayService  payService;
    @Value("${server.port}")
    private String port;
    @GetMapping("pay/get/info")
    @RefreshScope
//用来是的consul 配置 实时刷新。
    public String getInfo(@Value("${atguigu.info}") String atguiguInfo){
        System.out.println("afdgdaslkgjl");
        return "info:"+atguiguInfo + "port:" + port;
    }
    @PostMapping(value = "/pay/add")
    @Operation(summary = "新增",description = "新增支付流水方法,json串做参数")
    public ResultData<String> addPay(@RequestBody Pay pay){
        System.out.println(pay.toString());
        int i = payService.add(pay);
        return ResultData.success("成功插入记录，返回值："+i);
    }
    @DeleteMapping(value = "/pay/del/{id}")
    @Operation(summary = "删除",description = "删除支付流水方法")
    public ResultData<Integer> deletePay(@PathVariable("id") Integer id) {
        return ResultData.success(payService.delete(id));
    }
    @PutMapping(value = "/pay/update")
    @Operation(summary = "修改",description = "修改支付流水方法")
    public ResultData<String> updatePay(@RequestBody PayDTO payDTO){
        Pay pay = new Pay();
        BeanUtils.copyProperties(payDTO, pay);

        int i = payService.update(pay);
        return ResultData.success("成功修改记录，返回值："+i);
    }
    @RequestMapping("pay/getAll")
    @Operation(summary = "查询所有",description = "查询支付流水方法")

    public ResultData<List<Pay>> getAll()
    {
        return ResultData.success(payService.getAll());
    }
    @GetMapping(value = "/pay/get/{id}")
    @Operation(summary = "按照ID查流水",description = "查询支付流水方法")
    public ResultData<Pay> getById(@PathVariable("id") Integer id){
        Pay byId = payService.getById(id);
        if (byId.getId()==null){
            throw new RuntimeException();
        }
        return ResultData.success(byId);
    }//全部查询getall作为家庭作业

}
